package com.weiyi.listener;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/listener")
public class ListenerDemo extends HttpServlet{
	private Map<String,String> users;
	
	public ListenerDemo() {
		users = new HashMap<String,String>();
		users.put("weiyi2020","123456");
		users.put("Elodie","123456");
		users.put("momor","123456");
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		String page = "login.html";
		if(users.containsKey(username) && users.get(username).equals(password)) {
			req.getSession().setAttribute("user", username);
			page = "welcome.view";
		}
		resp.sendRedirect(page);
	}
}
